import CZML_GENERALTOOLS_CLASS from "./../main/CZML_GENERALTOOLS_CLASS.js"
import SMAP_LinkedList from "./../utils/SMAP_LinkedList.js"

class CZML_Axis {
  constructor(viewer, id, name, DataStartTime, idRef, posX, posY, posZ, LineWidth, LineColor, LineStyle, MarkerVisible, ReferenceFrame) {
    this._viewer = viewer
    this.id = id
    this.name = name
    this.DataStartTime = DataStartTime
    this.idRef = idRef
    this.posX = posX
    this.posY = posY
    this.posZ = posZ
    this.LineWidth = LineWidth
    this.LineColor = LineColor
    this.LineStyle = LineStyle
    this.MarkerVisible = MarkerVisible
    this.ReferenceFrame = ReferenceFrame
    this.sDataStartTime = CZML_GENERALTOOLS_CLASS.Utcg2ISO8601TimeTransformation(DataStartTime)
    this.PointXID = id + "-to-axis-pointx"
    this.PointYID = id + "-to-axis-pointy"
    this.PointZID = id + "-to-axis-pointz"
    this.LineXID = id + "-to-axis-linex"
    this.LineYID = id + "-to-axis-liney"
    this.LineZID = id + "-to-axis-linez"
    this.OriginRef = idRef + "#position"
    this.PointXRef = this.PointXID + "#position"
    this.PointYRef = this.PointYID + "#position"
    this.PointZRef = this.PointZID + "#position"
    this.PointX = {
      id: this.PointXID,
      name: name[0],
      availability: CZML_GENERALTOOLS_CLASS.HEU_ScenarioTime,
      show: true,
      label: {
        fillColor: {
          rgba: LineColor
        },
        font: "11pt Lucida Console",
        horizontalOrigin: "CENTER",
        pixelOffset: {
          cartesian2: [30, 10]
        },
        show: MarkerVisible,
        style: "FILL_AND_OUTLINE",
        text: name[0],
        verticalOrigin: "CENTER"
      },
      position: {
        interpolationAlgorithm: "LAGRANGE",
        interpolationDegree: 5,
        referenceFrame: ReferenceFrame,
        epoch: this.sDataStartTime,
        cartesian: posX
      }
    }
    this.PointY = {
      id: this.PointYID,
      name: name[1],
      availability: CZML_GENERALTOOLS_CLASS.HEU_ScenarioTime,
      show: true,
      label: {
        fillColor: {
          rgba: LineColor
        },
        font: "11pt Lucida Console",
        horizontalOrigin: "CENTER",
        pixelOffset: {
          cartesian2: [30, 10]
        },
        show: MarkerVisible,
        style: "FILL_AND_OUTLINE",
        text: name[1],
        verticalOrigin: "CENTER"
      },
      position: {
        interpolationAlgorithm: "LAGRANGE",
        interpolationDegree: 5,
        referenceFrame: ReferenceFrame,
        epoch: this.sDataStartTime,
        cartesian: posY
      }
    }
    this.PointZ = {
      id: this.PointZID,
      name: name[2],
      availability: CZML_GENERALTOOLS_CLASS.HEU_ScenarioTime,
      show: true,
      label: {
        fillColor: {
          rgba: LineColor
        },
        font: "11pt Lucida Console",
        horizontalOrigin: "CENTER",
        pixelOffset: {
          cartesian2: [30, 10]
        },
        show: MarkerVisible,
        style: "FILL_AND_OUTLINE",
        text: name[2],
        verticalOrigin: "CENTER"
      },
      position: {
        interpolationAlgorithm: "LAGRANGE",
        interpolationDegree: 5,
        referenceFrame: ReferenceFrame,
        epoch: this.sDataStartTime,
        cartesian: posZ
      }
    }
    this.LineX = {
      id: this.LineXID,
      name: name[0],
      availability: CZML_GENERALTOOLS_CLASS.HEU_ScenarioTime,
      polyline: {
        show: [
          {
            interval: CZML_GENERALTOOLS_CLASS.HEU_ScenarioTime,
            boolean: true
          }
        ],
        width: LineWidth,
        material: {
          solidColor: {
            color: {
              rgba: LineColor
            }
          }
        },
        followSurface: false,
        positions: {
          references: [this.OriginRef, this.PointXRef]
        }
      }
    }
    this.LineY = {
      id: this.LineYID,
      name: name[1],
      availability: CZML_GENERALTOOLS_CLASS.HEU_ScenarioTime,
      polyline: {
        show: [
          {
            interval: CZML_GENERALTOOLS_CLASS.HEU_ScenarioTime,
            boolean: true
          }
        ],
        width: LineWidth,
        material: {
          solidColor: {
            color: {
              rgba: LineColor
            }
          }
        },
        followSurface: false,
        positions: {
          references: [this.OriginRef, this.PointYRef]
        }
      }
    }
    this.LineZ = {
      id: this.LineZID,
      name: name[2],
      availability: CZML_GENERALTOOLS_CLASS.HEU_ScenarioTime,
      polyline: {
        show: [
          {
            interval: CZML_GENERALTOOLS_CLASS.HEU_ScenarioTime,
            boolean: true
          }
        ],
        width: LineWidth,
        material: {
          solidColor: {
            color: {
              rgba: LineColor
            }
          }
        },
        followSurface: false,
        positions: {
          references: [this.OriginRef, this.PointZRef]
        }
      }
    }
    this.CZML_AddAxis()
  }

  CZML_AddAxis() {
    // 首先判断idRef在场景中是否存在
    const idRef = this.idRef
    const FindObjectNode = CZML_GENERALTOOLS_CLASS.CzmlList.find(function (node) {
      return node.data().id === idRef
    })
    if (FindObjectNode == null) {
      return -1
    }

    // 虚线
    if (this.LineStyle === 1) {
      this.LineX.polyline.material = {
        polylineDash: {
          color: {
            rgba: this.LineColor
          }
        }
      }
      this.LineY.polyline.material = {
        polylineDash: {
          color: {
            rgba: this.LineColor
          }
        }
      }
      this.LineZ.polyline.material = {
        polylineDash: {
          color: {
            rgba: this.LineColor
          }
        }
      }
    }

    // 箭头
    if (this.LineStyle === 2) {
      this.LineX.polyline.material = {
        polylineArrow: {
          color: {
            rgba: this.LineColor
          }
        }
      }
      this.LineY.polyline.material = {
        polylineArrow: {
          color: {
            rgba: this.LineColor
          }
        }
      }
      this.LineZ.polyline.material = {
        polylineArrow: {
          color: {
            rgba: this.LineColor
          }
        }
      }
    }

    const ret1 = CZML_GENERALTOOLS_CLASS.InsertModelObjectInformation(this.PointXID, this.PointX, true)
    const ret2 = CZML_GENERALTOOLS_CLASS.InsertModelObjectInformation(this.PointYID, this.PointY, true)
    const ret3 = CZML_GENERALTOOLS_CLASS.InsertModelObjectInformation(this.PointZID, this.PointZ, true)
    const ret4 = CZML_GENERALTOOLS_CLASS.InsertModelObjectInformation(this.LineXID, this.LineX, true)
    const ret5 = CZML_GENERALTOOLS_CLASS.InsertModelObjectInformation(this.LineYID, this.LineY, true)
    const ret6 = CZML_GENERALTOOLS_CLASS.InsertModelObjectInformation(this.LineZID, this.LineZ, true)
    CZML_GENERALTOOLS_CLASS.Upper(this._viewer)
    return ret1 && ret2 && ret3 && ret4 && ret5 && ret6
  }
}

export default CZML_Axis
